package cn.com.fuhuitong.main.mine.ui.activity

import android.content.Intent
import androidx.lifecycle.Observer
import cn.com.fuhuitong.aroute.ARouteConstance
import cn.com.fuhuitong.main.base.ViewModeActivity
import com.alibaba.android.arouter.facade.annotation.Route
import cn.com.fuhuitong.R
import cn.com.fuhuitong.app.AppConstance
import cn.com.fuhuitong.base.EmptyContent
import cn.com.fuhuitong.http.HttpResponseStatus
import cn.com.fuhuitong.main.ItemViewType
import cn.com.fuhuitong.main.mine.adapter.AddressBottomAdapter
import cn.com.fuhuitong.main.mine.adapter.AddressListAdapter
import cn.com.fuhuitong.main.mine.vm.AddressViewModel
import cn.com.fuhuitong.main.mine.vm.MineViewModel
import cn.com.fuhuitong.main.order.ui.activity.OrderSubmitActivity
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.launcher.ARouter
import com.alibaba.android.vlayout.DelegateAdapter
import com.alibaba.android.vlayout.layout.LinearLayoutHelper
import com.alibaba.android.vlayout.layout.StickyLayoutHelper
import kotlinx.android.synthetic.main.activity_mine_address.*

@Route(path = ARouteConstance.ACTIVITY_URL_MINE_ADDRESS)
class MineAddressActivity : ViewModeActivity<AddressViewModel>() {

    @Autowired(name = AppConstance.BUNDLE_ADDRESS_TYPE)
    @JvmField
    var type: Int = 0

    private lateinit var addressListAdapter: AddressListAdapter

    override fun viewModel(): AddressViewModel = createViewModel()

    override val layoutResId: Int
        get() = R.layout.activity_mine_address

    override fun initView() {

        image_address_back.setOnClickListener {
            this.finish()
        }

        text_mine_address_add.setOnClickListener {
            ARouter.getInstance().build(ARouteConstance.ACTIVITY_URL_MINE_ADDRESS_EDIT)
                .withInt(AppConstance.BUNDLE_ADDRESS_TYPE, 0)
                .navigation()
        }

        this.initViewRecyclerAdapter(
            recycler_mine_address, arrayOf(
                ItemViewType.ADDRESS_LIST, ItemViewType.ADDRESS_BOTTOM
            )
        )

        delegateAdapter.addAdapter(
            AddressListAdapter(applicationContext, LinearLayoutHelper().apply {
                setDividerHeight(resources.getDimensionPixelSize(R.dimen.dp_10))
            }).apply {
                addressListAdapter = this
            }
        )

        addressListAdapter.setOnItemClickListener { view, i, address ->
            if (type != 0) {
                val intent = Intent(this, OrderSubmitActivity::class.java)
                intent.putExtra("address", address)
                setResult(RESULT_OK, intent)
                this.finish()
            }
        }
        addressListAdapter.setOnItemChildClickListener { view, i, addressEntity ->
            if (view.id == R.id.text_address_name_edit) {
                ARouter.getInstance().build(ARouteConstance.ACTIVITY_URL_MINE_ADDRESS_EDIT)
                    .withInt(AppConstance.BUNDLE_ADDRESS_TYPE, 1)
                    .withSerializable(AppConstance.BUNDLE_ADDRESS_ENTITY, addressEntity)
                    .navigation()
            }
        }
    }

    override fun initViewMode() {
        viewModel.addressListDataLiveData.observe(this, Observer { response ->
            if (response.status == HttpResponseStatus.Status.FAILURE) {
                toast(response.exception?.message ?: "未知错误")
                return@Observer
            }
            if (response.status != HttpResponseStatus.Status.SUCCESS)
                return@Observer
            response.response?.data?.let {
                addressListAdapter.setAddDataNew(it)
            }
        })
    }

    override fun onResume() {
        super.onResume()
        viewModel.addressList()
    }
}